<template>
    <div>
        <template v-if="Value.length">
            <div class="ItemView" v-for="(item, index) in Value">
                <div style="flex: 1;">
                    <DictionaryItem :Type="Type" :Option="Option" :Level="Level + 1" updateKey="" :index="index"
                        :Value="item.key" @UpdateValue="(val, index) => { UpdateValue(val, index, 'key') }" />
                </div>
                <div style="width: 10px; text-align: center;">:</div>
                <div style="flex: 1;">
                    <DictionaryItem :Type="Type" :Option="Option" :Level="Level + 1" :index="index" :Value="item.value"
                        @UpdateValue="(val, index) => { UpdateValue(val, index, 'value') }" />
                </div>
                <div class="Delete" @click="delect(index)">X</div>
            </div>
        </template>
        <div class="empty" v-if="!Value.length">
            暂无数据
        </div>
    </div>
</template>
<script setup>
import { ref } from 'vue';
import DictionaryItem from './DictionaryItem.vue'

const props = defineProps(['Option', 'DefaultValue', 'Level']);
const Value = ref([]);

function GetContent(Type) {
    var TypeArray = Type.split(':');
    TypeArray.shift();
    return TypeArray.join(':');
}

const Type = GetContent(props.Option.Type);


function UpdateValue(_Value, index, key) {
    Value.value[index][key] = _Value;
}

function delect(index) {
    Value.value.splice(index, 1)
}


const GetValue = function () {
    var data = {};
    for (let i = 0; i < Value.value.length; i++) {
        const element = Value.value[i];
        data[element.key] = element.value;
    }
    return data
}

const SetValue = function () { }

defineExpose({
    add: function () {
        Value.value.push({ key: '', value: '' });
    }, GetValue, SetValue
})




</script>
<style lang="less" scoped>
.ItemView {
    display: flex;
    padding-top: 10px;
    align-items: center;
}

.ItemName {
    width: 80px;

    text-align: right;
    padding-right: 10px;
}

.empty {
    padding: 10px;
    text-align: center;
}

.Delete {
    user-select: none;
    cursor: pointer;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    margin-left: 10px;
    background-color: red;
    color: #fff;
    text-align: center;
}
</style>